package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductResult;
import cn.itcast.vo.ExportResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ExtEproductDao extEproductDao;

    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
        List<String> idList = Arrays.asList(export.getContractIds().split(","));

        int eProNum = 0;
        int eExtNum = 0;
        String exportId = export.getId();
        // 货物信息数据迁移到报运货物信息
        ContractProductExample productExample = new ContractProductExample();
        productExample.createCriteria().andContractIdIn(idList);
        List<ContractProduct> productList = contractProductDao.selectByExample(productExample);
        for (ContractProduct product : productList) {
            eProNum += 1;
            String productId = product.getId();

            ExportProduct exportProduct = new ExportProduct();
            BeanUtils.copyProperties(product,exportProduct);
            exportProduct.setExportId(exportId);

            // 货物下的附件信息迁移到报运货物下的附件中
            ExtCproductExample cproductExample = new ExtCproductExample();
            cproductExample.createCriteria().andContractProductIdEqualTo(productId);
            List<ExtCproduct> cproductList = extCproductDao.selectByExample(cproductExample);
            eExtNum += cproductList.size();
            for (ExtCproduct cproduct : cproductList) {
                ExtEproduct eproduct = new ExtEproduct();
                BeanUtils.copyProperties(cproduct,eproduct);
                eproduct.setExportProductId(productId);
                eproduct.setExportId(exportId);
                extEproductDao.insertSelective(eproduct);
            }
            exportProductDao.insertSelective(exportProduct);
        }

        export.setProNum(eProNum);
        export.setExtNum(eExtNum);
        export.setState(0);
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andIdIn(idList);
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        StringBuilder sb = new StringBuilder();
        for (Contract contract : contractList) {
            sb.append(contract.getContractNo()).append(" ");

            // 更新每个合同状态为已报运（state==2）
            contract.setState(2);
            contractDao.updateByPrimaryKeySelective(contract);
        }
        export.setCustomerContract(sb.toString());
        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        exportDao.updateByPrimaryKeySelective(export);
        List<ExportProduct> exportProductList = export.getExportProducts();
        if (exportProductList!=null && exportProductList.size()>0) {
            for (ExportProduct exportProduct : exportProductList) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    public void delete(String id) {
        // 更新合同的状态为已上报，未报运状态（state==1）
        Export export = exportDao.selectByPrimaryKey(id);
        List<String> cIdList = Arrays.asList(export.getContractIds().split(","));
        for (String cId : cIdList) {
            Contract contract = new Contract();
            contract.setId(cId);
            contract.setState(1);
            contractDao.updateByPrimaryKeySelective(contract);
        }

        extEproductDao.deleteByExportId(id);
        extCproductDao.deleteByExportId(id);
        exportDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void updateE(ExportResult result) {
        Set<ExportProductResult> resultProducts = result.getProducts();
        for (ExportProductResult resultProduct : resultProducts) {
            ExportProduct exportProduct = new ExportProduct();
            exportProduct.setId(resultProduct.getExportProductId());
            exportProduct.setTax(resultProduct.getTax());
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }

        String exportId = result.getExportId();
        Export export = new Export();
        export.setId(exportId);
        export.setState(result.getState());
        export.setRemark(result.getRemark());
        exportDao.updateByPrimaryKeySelective(export);
    }
}
